package com.cy.personer.web;

import com.cy.personer.bean.Desk;
import com.cy.personer.bean.Role;
import com.cy.personer.bean.Work;
import com.cy.personer.biz.impl.DeskBizImpl;
import com.cy.personer.biz.impl.WorkBizImpl;
import com.cy.personer.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/manageWork")
public class ManageWorkHandler {
    @Resource
    private WorkBizImpl workBizImpl;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private DeskBizImpl deskBizImpl;
    @Resource
    private MD5Encoder md5Encoder;

    @RequestMapping(value="/showWork.action")
    public String showWork(HttpServletRequest request,String num,Work work) throws UnsupportedEncodingException {
        int pageNum = 1;
        if(StringUtils.isNotEmpty(num)){
            if(!num.equals("0")){
                pageNum = Integer.parseInt(num);
            }
//            pageNum = Integer.parseInt(num);
        }
        PageHelper.startPage(pageNum,5);
        List<Work> workList = workBizImpl.getWork(work);
        PageInfo<Work> pageInfo = new PageInfo<>(workList);
        request.setAttribute("pageInfo",pageInfo);

        HttpSession session = request.getSession();
        //查询科室
        List<Desk> DeskAll22 = deskBizImpl.findAllDesk();
//        request.setAttribute("DeskAll",DeskAll);
        session.setAttribute("DeskAll22",DeskAll22);
        //查询角色
        List<Role> RoleAll22 = roleMapper.query();
//        request.setAttribute("RoleAll",RoleAll);
        session.setAttribute("RoleAll22",RoleAll22);
        return "admin/workList";
    }

    //修改内部人员的状态
    @RequestMapping(value="/updataState.action")
    public String updataState(String workId,String stateId){
        String result = "redirect:/manageWork/showWork.action";
        int workId1 = Integer.parseInt(workId);
        int stateId1 = Integer.parseInt(stateId);
        boolean result1 = workBizImpl.updataState(workId1,stateId1);
        if (result1){
            result = "redirect:/manageWork/showWork.action";
        }
//        return "redirect:/manageWork/showWork.action";//从一个action跳到另一个action
        return result;
//        return "forward:/manageWork/showWork.action";
    }

    //展示添加内部工作人员页面
    @RequestMapping(value="/showAddWork.action")
    public String showAddWork(){
        return "admin/addWork";
    }

    //验证用户是否可用
    @RequestMapping(value="/accountCheck.action")
    public @ResponseBody String accountCheck(String account){
        String result = "error";
        Work work = workBizImpl.accountCheck(account);
        if(work!=null){
            result = "success";
        }
        return result;
    }

    //增加内部工作人员
    @RequestMapping(value="/addWork.action")
    public @ResponseBody String addWork(Work work) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result1 = "error";
        //日期
        Date day = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd  HH:mm:ss" );
        String str = sdf.format(day);
        work.setRegisterTime(str);
        work.setStateId(1);
        String password = md5Encoder.EncoderByMd5(work.getWorkPwd());
        work.setWorkPwd(password);
        boolean result = workBizImpl.addWork(work);
        if (result){
            result1 = "success";
        }
        return result1;
    }

    //展示编辑用户
    @RequestMapping(value="/showEeitWork.action")
    public String showEeitWork(HttpServletRequest request,Integer workId){
        String result = "admin/workList";
        Work showWork = workBizImpl.findWork(workId);
        if(showWork != null){
            request.setAttribute("showWork",showWork);
            result = "admin/editWork";
        }
        return result;
    }

    //编辑用户信息
    @RequestMapping(value="/eitWork.action")
    public @ResponseBody String eitWork(Work work){
        String result1 = "error";
        boolean result = workBizImpl.eitWork(work);
        if (result){
            result1 = "success";
        }
        return result1;
    }

    //展现修改密码页面
    @RequestMapping(value="/showChangeWorkPwd.action")
    public String showChangeWorkPwd(HttpServletRequest request,Integer workId){
        String result = "admin/workList";
        Work showWork = workBizImpl.findWork(workId);
        if(showWork != null){
            request.setAttribute("showWork",showWork);
            result = "admin/changeWorkPwd";
        }
        return result;
    }

    //修改密码页面
    @RequestMapping(value="/changeWorkPwd.action")
    public @ResponseBody String changeWorkPwd(Work work) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result1 = "error";
        String password = md5Encoder.EncoderByMd5(work.getWorkPwd());
        work.setWorkPwd(password);
        boolean result = workBizImpl.changeWorkPwd(work);
        if (result){
            result1 = "success";
        }
        return result1;
    }

    //展示个人信息页面
    @RequestMapping(value="/showMyInformation.action")
    public String showMyInformation(HttpServletRequest request,Integer workId){
        String result = "admin/workList";
        Work showWork = workBizImpl.findWork(workId);
        if(showWork != null){
            request.setAttribute("showWork",showWork);
            result = "admin/myInformation";
        }
        return result;
    }



    public WorkBizImpl getWorkBizImpl() {
        return workBizImpl;
    }

    public void setWorkBizImpl(WorkBizImpl workBizImpl) {
        this.workBizImpl = workBizImpl;
    }

    public RoleMapper getRoleMapper() {
        return roleMapper;
    }

    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    public DeskBizImpl getDeskBizImpl() {
        return deskBizImpl;
    }

    public void setDeskBizImpl(DeskBizImpl deskBizImpl) {
        this.deskBizImpl = deskBizImpl;
    }
}
